"""
学生视图层
"""
from lib import common
from interface import student_interface
from interface import common_interface
#登陆状态
student_dic = {
    "user":None
}
#注册功能
def regiter():
    while True:
        student_name = input("请输入学生的名字:")
        student_password = input("请输入学生的密码:")
        student_repassword = input("请再输入学生的密码:")
        if student_password != student_repassword:
            print("俩次密码不同，请重新输入！")
            continue
        flag,msg = student_interface.regiter(student_name,student_password)
        if flag:
            print(msg)
            break
        else:
            print(msg)
#登陆功能
def login():
    while True:
        name = input("请输入用户账号:")
        password = input("请输入用户密码:")
        flag,msg = common_interface.login(name,password,type = "student")
        if flag:
            student_dic["user"] = name
            print(msg)
            break
        else:
            print(msg)

#选择学校
@common.login("student")
def chioce_school():
    while True:
        flag,msg = common_interface.select_school()
        if not flag:
           print(msg)
        school = msg
        for index,school_name in enumerate(school):
            print(f"编号:{index},学校名字:{school_name}")
        chioce = input("请按着编号选择需要添加的校区:")
        if not chioce.isdigit():
            print("请输入正确的编号!")
            continue 
        chioce = int(chioce)
        if chioce not in range(len(school)):
            print("请输入正确的编号")
            continue
        flag,msg = student_interface.add_school(school[chioce],student_dic["user"]) 
        if flag:
            print(msg)
            break
        else:
            print(msg)



#选择课程
@common.login("student")
def chioce_crouse():
    while True:
        #先获取学生所在学校的课程列表
        flag,msg = common_interface.select_crouse(student_dic["user"])
        if not flag:
            print(msg)
            break
        crouse_list = msg
        for index,crouse_name in enumerate(crouse_list):
            print(f"编号:{index}===课程名:{crouse_name}")
        choice = input("请选择要添加的课程编号:")
        if not choice.isdigit():
            print("请输入正确的编号!")
            continue
        choice = int(choice) 
        if choice not in range(len(crouse_list)):
            print("请输入正确的编号!")
            continue
        crouse_name = crouse_list[choice]
        flag,msg = student_interface.add_crouse(crouse_name,student_dic["user"])             
        if flag:
            print(msg)
            break
        else:
            print(msg)
#查看分数
@common.login("student")
def select_sroce():
    flag,msg = student_interface.check_sorce(student_dic["user"])
    if flag:
        print(msg)
    else:
        print(msg)

func_dic={
    "1":regiter,
    "2":login,
    "3":chioce_school,
    "4":chioce_crouse,
    "5":select_sroce,
}


def student_view():
    while True:
        print("""
        =================
            1.注册
            2.登录功能
            3.选择校区
            4.选择课程
            5.查看分数
        =================
        """)
        choice = input("请按编号选择一个功能(输入q退出此功能):")
        if choice == "q":
            break       
        if choice  not in func_dic:
            print("请输入正确得编号")
            continue
        func_dic.get(choice)()